package com.gszcn.tools.table.metadata;

import com.gszcn.tools.table.metadata.javadoc.SqlJSON;

import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

/**
 * 按类文件读写
 */
public class SqlMetadata {
    private static ProcessingEnvironment environment;

    public static Set <Element> EntityTable = new HashSet <Element>();
    /**
     * 文件位置
     */
    static final String METADATA_PATH = "META-INF/comment-sql/";
    /**
     * 存放位置
     */
    private static final String RESOURCES_FOLDER = "resources";
    /**
     * 编译位置
     */
    private static final String CLASSES_FOLDER = "classes";

    /**
     * 环境注入
     *
     * @param processingEnv
     */
    public SqlMetadata(ProcessingEnvironment processingEnv) {
        this.environment = processingEnv;

    }

    public static void setProcess(Set <? extends TypeElement> annotations, RoundEnvironment env) {
        for (TypeElement annotation : annotations) {
            System.out.println("annotation:" + annotation);
            for (Element e : env.getElementsAnnotatedWith(annotation)) {
                EntityTable.add(e);
            }
        }
        ArrayList <SqlJSON> sqlJSONS = new ArrayList <>();
        for (Element element : EntityTable) {
            SqlMetar sqlMetar = new SqlMetar(element, environment);
            sqlJSONS.add(sqlMetar.getSqlJSON());
        }
        /**
         * 写入模式
         * COMMENT ON COLUMN AC01.AAB004 IS 'wer';
         */
        sqlJSONS.forEach(s -> {
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append("COMMENT ON TABLE "+s.getClassname()+" IS '"+s.getComment()+"';\n");
            s.getSqlItems().forEach(sql -> {
                if (sql.getType().matches("String|Long|long|Integer|int|Date|float|Float|byte|boolean|Boolean|double|short")) {
                    stringBuffer.append("COMMENT ON COLUMN " + s.getClassname() + "." + sql.getName() + " IS '" + sql.getComment() + "';\n");
                }
            });
            filedw(s.getClassname(), stringBuffer);
        });


    }

    private static void filedw(String entityname, StringBuffer buffer) {
        try {
            URL resource = SqlMetadata.class.getResource("/");
            File file = new File(resource.getPath() + METADATA_PATH + entityname + ".sql");
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            FileWriter fileWritter = new FileWriter(file.getAbsolutePath(), false);
            BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
            bufferWritter.write(buffer.toString());
            bufferWritter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
